package org.jeecg.modules.FinanceModule.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.FinanceModule.entity.FabricDzEntity;
import org.jeecg.modules.FinanceModule.entity.ReconciliationEntity;
import org.jeecg.modules.FinanceModule.entity.InvoiceEntity;
import org.jeecg.modules.FinanceModule.entity.SupplierEntity;
import org.jeecg.modules.FinanceModule.mapper.SupplierMapper;
import org.jeecg.modules.FinanceModule.service.SupplierService;
import org.jeecg.modules.orderDistributeLeafletsModule.entity.sysuserOrderEntity;
import org.jeecg.modules.zonglzp.entity.ProductEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    SupplierMapper mapper;

    @Override
    public IPage<SupplierEntity> getSupplierList(Page<SupplierEntity> page, String companyName, String id, String workNo) {
        return mapper.getSupplierList(page,companyName,id,workNo);
    }

    //查询供应商的对账表
    @Override
    public IPage<SupplierEntity> getduizhangList(Page<ReconciliationEntity> page, String supplierId,String starttime,String endtime) {
        return mapper.getduizhangList(page, supplierId,starttime,endtime);
    }

    //供应商对账详情 上面框
    @Override
    public SupplierEntity getduizhangdetail(String supplierId) {
        SupplierEntity entity = mapper.getduizhangdetail(supplierId);

        //订单面料对账金额
        String mianliaoduizhangmoney=mapper.getmianliaoduizhangmoney(supplierId);
        if (mianliaoduizhangmoney == null) {
            mianliaoduizhangmoney = "0.0";
        }

        //查询供应商所有的已对账成本项
        String chengbengxiangduizhangmoney=mapper.getchengbengxiangduizhangmoney(supplierId);
        if (chengbengxiangduizhangmoney == null) {
            chengbengxiangduizhangmoney = "0.0";
        }

        //查询供应商已对账的工艺售价
        String gongyiduizhangmoney=mapper.getgongyiduizhangmoney(supplierId);
        if (gongyiduizhangmoney == null) {
            gongyiduizhangmoney = "0.0";
        }

        //查询供应商已对账的返修
        String fanxuiduizhangmoney=mapper.getfanxuiduizhangmoney(supplierId);
        if (fanxuiduizhangmoney == null) {
            fanxuiduizhangmoney = "0.0";
        }
        //查询对账总金额
//        String duizhangmoney = mapper.getduizhangmoney(supplierId);
//        if (duizhangmoney == null) {
//            duizhangmoney = "0.0";
//        }
        entity.setDuizhangmoney(String.valueOf(Double.parseDouble(mianliaoduizhangmoney)+Double.parseDouble(chengbengxiangduizhangmoney)+Double.parseDouble(gongyiduizhangmoney)+Double.parseDouble(fanxuiduizhangmoney)));

        //查询总条数
        int ordernums = mapper.getordernums(supplierId);
        entity.setOrdernums(ordernums);


        //这里要查询供应商的已开票记录和未开票记录
        //已开票
        String yikaipiaomoney=mapper.getSupplieryiInvoicemoney(supplierId,1);
        if(yikaipiaomoney==null)
            yikaipiaomoney="0.0";
        entity.setYiInvoicemoney(yikaipiaomoney);
        //未开票
        String weikaipiaomoney=mapper.getSupplieryiInvoicemoney(supplierId,0);
        if(weikaipiaomoney==null)
            weikaipiaomoney="0.0";
        entity.setWeiInvoicemoney(weikaipiaomoney);
        return entity;
    }

    //
    @Override
    public IPage<InvoiceEntity> getSupplierkaipiaoList(Page<InvoiceEntity> page, String supplierId) {
        return mapper.getSupplierkaipiaoList(page, supplierId);
    }


    //查询已有对账记录的供应商列表
    @Override
    public IPage<SupplierEntity> getyiReconciliationSupplierList(Page<SupplierEntity> page, String companyName, String id,String starttime,String endtime) {
        return mapper.getyiReconciliationSupplierList(page, companyName, id, starttime, endtime);
    }


    //选择好供应商后，让他们在这段时间内的对账全部通过
    @Override
    public void approveoftimeduizhang(List<String> ids, String starttime, String endtime) {
         for(int i=0;i<ids.size();i++){
             mapper.approveoftimeduizhang(ids.get(i),starttime,endtime);
         }
    }

    @Override
    public IPage<SupplierEntity> temporaryduizhangSupplierList(Page<SupplierEntity> page, String[] ids) {
        String idsString="";
        for(int i=0;i<ids.length;i++){
            idsString=idsString+"\""+ids[i]+"\"";
            if(i!=ids.length-1)
                idsString=idsString+",";
        }
//        String idsString= StringUtils.join(ids, ",");
        return mapper.temporaryduizhangSupplierList(page, idsString);
    }


    //修改结算金额
    @Override
    public int updatesettlementMoney(ReconciliationEntity entity) {
        return mapper.updatesettlementMoney(entity);
    }

    //查询这个供应商下所有订单的面料
    @Override
    public IPage<FabricDzEntity> getSysuserAllOrderFabric(Page<FabricDzEntity> page, String sysuserId, String starttime, String endtime) {
        IPage<FabricDzEntity> prodList=mapper.getSysuserAllOrderFabric(page,sysuserId, starttime, endtime);
        if(prodList!=null){
            for (int i=0;i<prodList.getRecords().size();i++){
                //分解 面料id和米数那个字段 0存面料id  1存米数
                if(prodList.getRecords().get(i).getFabricIds()!=null){
                    String[] fabricdetail = prodList.getRecords().get(i).getFabricIds().split("/");
                    HashMap<String,BigDecimal> fabric=mapper.getfabricbyid(Integer.parseInt(fabricdetail[0]));
                    String mianliaoCode=mapper.getmianliaoCodebyId(Integer.parseInt(fabricdetail[0]));
                    //赋值面料编号
                    prodList.getRecords().get(i).setMianliaoCode(mianliaoCode);
                    //赋值米数
                    prodList.getRecords().get(i).setMishu(fabricdetail[1]);
                    if(fabric!=null){
                        double chengbenMoney = Double.parseDouble(fabricdetail[1]) * fabric.get("fabric_price").doubleValue();
                        prodList.getRecords().get(i).setCostMoney(chengbenMoney);
                    }
                }
                prodList.getRecords().get(i).setChengbenxiang("面料");
            }
        }
        return prodList;
    }

    //修改产品的结算金额
    @Override
    public int updateProdsettleMoney(String prodId, String fabricSettleMoney, String type) {
        if(type.equals("1")){
            return mapper.updateProdsettleMoney(prodId,fabricSettleMoney);
        }else if(type.equals("2")) {
            return mapper.updatechengbengxiangMoney(Integer.parseInt(prodId),fabricSettleMoney);
        }else if(type.equals("3")){
            return mapper.updategongyiMoney(Integer.parseInt(prodId),fabricSettleMoney);
        }else {
            return mapper.updaterepairMoney(Integer.parseInt(prodId),fabricSettleMoney);
        }

    }

    /**
     * 查询供应商下所有订单的品类成本
     * @param page
     * @param sysuserId
     * @param starttime
     * @param endtime
     * @return
     */
    @Override
    public IPage<FabricDzEntity> getSysuserPingleicost(Page<FabricDzEntity> page, String sysuserId, String starttime, String endtime) {
        return mapper.getSysuserPingleicost(page,sysuserId,starttime,endtime);
    }

    /**
     * 查询供应商下所有订单的售价工艺对账
     * @param page
     * @param sysuserId
     * @param starttime
     * @param endtime
     * @return
     */
    @Override
    public IPage<FabricDzEntity> getSysuserpaidangongyi(Page<FabricDzEntity> page, String sysuserId, String starttime, String endtime) {
        return mapper.getSysuserpaidangongyi(page,sysuserId,starttime,endtime);
    }

    /**
     * 供应商对账的三部操作
     * @param ids
     * @param starttime
     * @param endtime
     */
    @Override
    public void duizhangWork(List<String> ids, String starttime, String endtime) {
        for(int i=0;i<ids.size();i++){
            //----------------------这里面料操作start
            //先查询这个供应商的所有派单
            List<sysuserOrderEntity> sysuserOrderList=mapper.getsysuserOrderList(ids.get(i),starttime,endtime);
            if(sysuserOrderList==null)
                continue;
            //循环取到查到的供应商订单
            for(int j=0;j<sysuserOrderList.size();j++){
                //通过订单查询产品,然后修改产品状态

               //  List<ProductEntity>   prodList=mapper.getProductByorderSn(sysuserOrderList.get(j).getOrderSn());
                List<ProductEntity>   prodList=mapper.getProductByProductId(sysuserOrderList.get(j).getProductId());
                 if(prodList!=null)
                 for(int z=0;z<prodList.size();z++){
                     //将产品成本计算出来
                     if( prodList.get(z).getFabricIds()==null)
                         continue;
                     String[] fabricdetail = prodList.get(z).getFabricIds().split("/");

                     //查询面料组是否为空，空就为主推面料
                     String gourpid=mapper.getgourpidbyfabricId(fabricdetail[0]);
                      if(gourpid==null){
                          HashMap<String,BigDecimal> fabric=mapper.getfabricbyid(Integer.parseInt(fabricdetail[0]));

                          double chengbenMoney = Double.parseDouble(fabricdetail[1]) * fabric.get("fabric_price").doubleValue();
                          //修改产品的对账信息
                          mapper.updateproductduizhang(prodList.get(z).getId(),String.valueOf(chengbenMoney));
                      }else{
                          //通过面料id查询他的面料组中的价格
                          String fabric_price=mapper.getgrouppricebyfabricid(fabricdetail[0]);

                          double chengbenMoney=Double.parseDouble(fabricdetail[1]) *Double.parseDouble(fabric_price);
                          //修改产品的对账信息
                          mapper.updateproductduizhang(prodList.get(z).getId(),String.valueOf(chengbenMoney));
                      }



                 }
            }
            //----------------------这里面料操作end

            //这里是成本项目 start
             List<FabricDzEntity> chengbenxiangList=mapper.getchengbenxiangList(ids.get(i),starttime,endtime);
            if(chengbenxiangList!=null)
            for(int h=0;h<chengbenxiangList.size();h++){
                //修改成本项目数据信息
                mapper.updatechengbenduizhang(chengbenxiangList.get(h).getId(),chengbenxiangList.get(h).getPrice());
            }
            //这里是成本项目 end

            //接下来是工艺
            List<FabricDzEntity> gongyiduizhangList=mapper.getgongyiduizhangList(ids.get(i),starttime,endtime);
                if(gongyiduizhangList!=null)
                for(int h=0;h<gongyiduizhangList.size();h++){
                    //修改成本项目数据信息
                    mapper.updategongyiduizhang(gongyiduizhangList.get(h).getId(),gongyiduizhangList.get(h).getCostPrice());
                }
            //这里是工艺 end

            //接下来是返修
            List<FabricDzEntity> repairduizhangList=mapper.getrepairduizhangList(ids.get(i),starttime,endtime);
                if(repairduizhangList!=null)
                    for(int x=0;x<repairduizhangList.size();x++){
                        mapper.updaterepairduizhang(repairduizhangList.get(x).getId(),repairduizhangList.get(x).getCostPrice());
                    }
            //这里是返修 end
        }

    }

    /**
     * 查询这个供应商的派单返修
     * @param page
     * @param sysuserId
     * @param starttime
     * @param endtime
     * @return
     */
    @Override
    public IPage<FabricDzEntity> getSysuserpaidanrepair(Page<FabricDzEntity> page, String sysuserId, String starttime, String endtime) {
        return mapper.getSysuserpaidanrepair(page,  sysuserId,  starttime,  endtime);
    }

}
